/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.misc.cg.descriptor;

import org.vacoor.nothing.common.util.StringUtils;

/**
 * One To Many 关联
 *
 * @author vacoor
 */
public class OneToMany extends ToMany {
    public OneToMany(String source) {
        super(source);
    }

    public String getComputedJoinColumn() {
        String relCol = null;
        // 如果关系由对方维护
        if (StringUtils.hasText(mappedBy)) {
            for (ManyToOne manyToOne : rel.getManyToOne()) {
                if (mappedBy.equals(manyToOne.getProperty())) {
                    relCol = manyToOne.getJoinColumn();
                    break;
                }
            }
            if (!StringUtils.hasText(relCol)) {
                throw new IllegalArgumentException(property + "标注为 OneToMany mappedBy=" + mappedBy + "" + ", 但是不能在" + rel.getFullTypeName() + "中发现 ManyToOne 属性:" + mappedBy + " @" + source);
            }
        } else {
            relCol = getJoinColumn();
        }
        return relCol;
    }
}
